<?php
include ("../conn.php");

$id="";
if(isset($_GET['id']))	$id = $_GET['id'];

$parentid="";
if(isset($_GET['parentid']))	$parentid=$_GET['parentid'];
$menuname="";
if(isset($_GET['menuname']))	$menuname=$_GET['menuname'];

$pparentid="";
if(isset($_GET['pparentid']))	$pparentid=$_GET['pparentid'];
$parentname="";
if(isset($_GET['parentname']))	$parentname=$_GET['parentname'];


$tip = "";
//保存或修改产品信息
if(isset($_POST['submit'])){
	//要传入 主键 和外键
	$id = $_POST['id'];
	$tablename = $databasepre."general";
	$insertStr1 = "";
	$insertStr2 = "";
	$updateStr = "";
	$content = "";

	$selectSQL = "select lower(column_name) as column_name ,lower(data_type) as column_type,character_maximum_length as column_length  from  information_schema.columns where table_name='".$tablename."' and table_schema='".$dbname."'";
	//echo $selectSQL;
	
	$query = mysql_query($selectSQL);
	$i = 0;
	while($row=mysql_fetch_array($query))
	{
		$sep = ",";
		$column_type = $row['column_type'];
		$column_name = $row['column_name'];
		$column_value = "";
		if(isset($_POST["$column_name"])){
		//echo "==".$column_name;
		$column_value = mysql_real_escape_string($_POST["$column_name"]);
	}
	
	if(isset($_POST["$column_name"]) ){//isset($_POST["$column_name"])  && $column_value!=""
			if($i==0){
				$i +=1;
				$sep = "";
			}
			$insertStr1 .= $sep.$column_name;
			if($column_type=="varchar" || $column_type=="text" || $column_type=="datetime"|| $column_type=="date"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
				
				//echo $column_name;
				if($column_name=="text1"){
					//echo $column_name;
					$content = $column_value;
				}
			}else if($column_type == "int"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
			}
		}
	}
	if($id==0 || $id==""){
		$insertSQL = "insert into ".$tablename."(".$insertStr1.",createdate) values (".$insertStr2.",now())";
		//echo $insertSQL;
		mysql_query($insertSQL);
		$id = mysql_insert_id();
		//保存图片路径
		
	}else{
		$updateSQL = "update ".$tablename." set ".$updateStr.",updatedate=now() where id=".$id;
		//echo $updateSQL;
		mysql_query($updateSQL);
		//保存图片路径
	}
	//保存图片路径
	$content = str_replace("\\","",$content);
	preg_match_all("/<img.*src=\"(.*)\".*\/>/isU",$content,$match);
	//print_r($match[1]);
	$srcArray = $match[1];
	
	for($k=0; $k<count($srcArray); $k++){
		//echo $srcArray[$k];
		
		//先判断是否已存在
		$sql="select * from ".$databasepre."file where fpath='".$srcArray[$k]."' and parentid='".$id."'";
		$query=mysql_query($sql);
		$row=mysql_fetch_array($query);
		if(empty($row)){
			//入库
			$sql =  "insert into ".$databasepre."file(fpath,seq,parentid,createdate) values ('".$srcArray[$k]."','".$k."',".$id.",now()) ";
			//echo $sql;
			mysql_query($sql);
		}
		
	}
	
	$tip ="保存成功！";
}

$sql="select * from ".$databasepre."general where id='".$id."'";
$query=mysql_query($sql);
$row=mysql_fetch_array($query);
?>
<html><head>
<meta http-equiv=Content-Type content=text/html;charset=utf-8>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
<script charset="utf-8" src="../plugin/jquery.min.1.3.2.js"></script>
<script charset="utf-8" src="../plugin/common.js"></script>
<script charset="utf-8" src="../plugin/kindeditor-4.0.6/kindeditor.js"></script>


<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #EEF2FB;
}
-->
</style>
</head>
<body>

<form method=POST action="" onSubmit="return isValid();">

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td valign="top" background="images/content-bg.gif">
		<table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
		  <tr><td height="31"><div class="titlebt"><?php echo $parentname;?></div></td></tr>
		</table>
	</td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9">
	<!-- 网页内容部分开始 -->


	<table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr><td height="13" valign="top">&nbsp;</td></tr>
	  <tr><td class="left_txt">当前位置：<?php echo $parentname;?> > <?php echo $menuname;?> </td></tr>
	  <!-- 一根线
	  <tr><td height="20"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"><tr><td></td></tr></table></td></tr>
	   -->
	  <!-- 说明信息
	  <tr><td><table width="100%" height="55" border="0" cellpadding="0" cellspacing="0"><tr>
				<td width="10%" height="55" valign="middle"><img src="images/title.gif" width="54" height="55"></td>
				<td width="90%" valign="top"><span class="left_txt2">在这里，您可以根据您的网站要求，修改设置网站的基本参数！</span><br>
				</td>
				</tr></table>
		</td>
	  </tr> 
	  -->
	  <tr><td class="left_txt">当前状态：
	  <?php if($id==0 || $id==""){ echo "新增";}else{ echo "编辑【".$row['field11']."】
	  				<a href='admin_wineno_one.php?act=create&parentid=".$parentid."&pparentid=".$pparentid."&parentname=".urlencode($parentname)."&menuname=".urlencode($menuname)."'
					style=' text-decoration:underline;color:green'>继续新增</a>";}?>
					
					<a href="admin_wineno_page.php?parentid=<?php echo $parentid;?>&pparentid=<?php echo $pparentid;?>&menuname=<?php echo urlencode($menuname);?>&parentname=<?php echo urlencode($parentname);?>" 
						target="main" style=' text-decoration:underline;color:green'>返回</a>
					</td></tr>
	  <tr><td><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
				  <tr><td class="left_bt2">&nbsp;&nbsp;&nbsp;&nbsp;
				  
				  <input type="submit" value=" 确 定 " name=submit  />   
				  <input type="reset" value=" 设 置 " />
				  <span id="tip_id" style='color:#FF0000; font-size:16px; font-family:"微软雅黑","黑体","宋体"'><?php echo $tip; ?></span>
				  </td>
				  </tr>
				</table>
		</td>
      </tr>
	  <tr><td>
	  

<input name="parentid" value="<?php echo $parentid;?>" type="hidden"/>
<input name="pparentid" value="<?php echo $pparentid;?>" type="hidden"/>

<input name="id" value="<?php echo $row['id'];?>" type="hidden"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="left">
	<tr>
		<td width="130px;">酒名称</td>
		<td><input type="text" id="field11" name="field11" size="60" value="<?php echo $row['field11'];?>" /></td>
		<td >(*必填)</td>
	</tr>
	<tr>
		<td width="130px;">酒标号NO</td>
		<td><input type="text" id="field11" name="field12" size="60" value="<?php echo $row['field12'];?>" /></td>
		<td >(*必填)</td>
	</tr>
	<tr>
		<td width="130px;">查询提示内容</td>
		<td><textarea  id="text1" name="text1" style="width:395px;height:105px;"><?php echo $row['text1'];?></textarea></td>
		<td >(*必填)</td>
	</tr>
	<tr>
		<td width="130px;">初始化被查询次数</td>
		<td><input type="text" id="int1" name="integer1" size="10" value="<?php  if(empty($row['integer1'])){echo "0";}else{echo $row['integer1'];} ?>" /></td>
		<td >(*必填)</td>
	</tr>
	

</table>


			  
			</td>
		</tr>
	 </table>


	<!-- 网页内容部分结束 -->
	</td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="bottom" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
    <td background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17"></td>
    <td valign="bottom" background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
function isValid(){
	var title = jQuery("#field11").val();
	var no = jQuery("#field12").val();
	
	if(JSTOOL.common.isBlank(title)){
		//jQuery("#tip_id").html("请输入标题！");
		//return false;
	}
	if(JSTOOL.common.isBlank(no)){
		jQuery("#tip_id").html("请输就编号！");
		return false;
	}
	
	
	return true;
}
</script>